/* --------------------------------------------------------
 * Copyright (c) aeky , Inc.  All rights reserved.
 * --------------------------------------------------------
 */
package com.aeky.server.support;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * 工具类 可以提供访问HttpServletRequest、HttpServletResponse，基于ThreadLocal实现
 * 
 * @author aeky
 */
public final class ServletUtils {

    /**
     * prevent constructing instance.
     */
    private ServletUtils() {
    }

    /** The servlet request. */
    private static ThreadLocal<HttpServletRequest> servletRequest = new ThreadLocal<HttpServletRequest>();

    /** The servlet response. */
    private static ThreadLocal<HttpServletResponse> servletResponse = new ThreadLocal<HttpServletResponse>();

    /**
     * Adjusts HTTP headers so that browsers won't cache response.
     * 
     * @param response For more background see <a
     *            href="http://www.onjava.com/pub/a/onjava/excerpt/jebp_3/index2.html"
     *            >this</a>.
     */
    public static void disableResponseCaching(HttpServletResponse response) {
        response.setHeader("Expires", "Sat, 1 January 2000 12:00:00 GMT");
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
    }

    /**
     * Return the request which invokes the service. Valid only if used in the
     * dispatching thread.
     * 
     * @return the servlet request
     */
    public static HttpServletRequest getRequest() {
        return servletRequest.get();
    }

    /**
     * Return the response which accompanies the request. Valid only if used in
     * the dispatching thread.
     * 
     * @return the servlet response
     */
    public static HttpServletResponse getResponse() {
        return servletResponse.get();
    }

    /**
     * Assign the current servlet request to a thread local variable. Valid only
     * if used inside the invoking thread scope.
     * 
     * @param request request
     */
    public static void setRequest(HttpServletRequest request) {
        servletRequest.set(request);
    }

    /**
     * Assign the current servlet response to a thread local variable. Valid
     * only if used inside the invoking thread scope.
     * 
     * @param response response
     */
    public static void setResponse(HttpServletResponse response) {
        servletResponse.set(response);
    }

    public static Locale getLocale() {
        HttpServletRequest requset = getRequest();
        String languge = requset.getParameter("locale");
        Locale locale = null;
        if (languge != null || !"".equals(languge)) {
            try {
                locale = new Locale(languge);
            } catch (Exception e) {
                locale = new Locale("zh");
            }
        }
        return locale == null ? requset.getLocale() : locale;
    }

}
